/**
 * 
 */
package registnet.courseDemand.extras;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.apache.torque.Torque;
import org.apache.torque.TorqueException;

import registnet.dao.CourseView;
import registnet.dao.CourseViewPeer;
import registnet.dao.StudentView;
import registnet.dao.StudentViewPeer;

/**
 * @author Dexter Ryan Carrasco
 */
public class RandomDataGenerator {
	public static void main(final String args[]) {
		// initialize Torque properties
		try {
			Torque.init("Torque.properties");
		} catch (final TorqueException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		final int numberOfStudents = 9000;
		final List<StudentView> students = StudentViewPeer
				.getStudentsWithAnyDegree();
		final List<String> scheduledStudents = new ArrayList<String>();
		final List<CourseView> subjects = CourseViewPeer.getSubjectsList();
		final int[] unitsOpts = { 6, 9, 12, 15, 17, 18, 19, 21 };
		final Random rand = new Random(System.currentTimeMillis());
		final int studentsSize = students.size();
		final int subjectsSize = subjects.size();
		final File outFile = new File(
				"C:/Documents and Settings/Dexter Ryan Carrasco/Desktop/RANDOM_WRITEIN_DATA_3.csv");

		try {
			final BufferedWriter output = new BufferedWriter(new FileWriter(
					outFile));

			for (int i = 0; i < numberOfStudents; i++) {
				final int units = unitsOpts[rand.nextInt(unitsOpts.length)];
				final String studentNumber = students.get(
						rand.nextInt(studentsSize)).getStudentNumber();
				int totalUnits = 0;

				if (!scheduledStudents.contains(studentNumber)) {
					scheduledStudents.add(studentNumber);
				} else {
					i--;
					continue;
				}

				final List<String> schedCourses = new ArrayList<String>();
				while (units != totalUnits) {
					final CourseView course = subjects.get(rand
							.nextInt(subjectsSize));
					final int credit = course.getCredit();

					if (course.getCourseCode().contains("BLK")) {
						continue;
					}

					if (credit + totalUnits <= units) {
						if (schedCourses.contains(course.getCourseCode())) {
							continue;
						} else {
							schedCourses.add(course.getCourseCode());
						}
						output.write(studentNumber + ","
								+ course.getCourseCode() + "," + credit);
						output.newLine();

						totalUnits += credit;
					}

				}
			}

			output.close();
		} catch (final IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
